package mk.timska_rabota_android;

import java.io.IOException;
import android.provider.Settings.Secure;
import mk.timska_rabota_android.location.LocationModule;
import mk.timska_rabota_android.messages.MessagesModule;
import mk.timska_rabota_android.SendMessage;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;

import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;

import org.apache.http.protocol.HTTP;

import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.Button;

import android.widget.Toast;

public class AddContactClass extends Activity implements OnClickListener {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_contact);
        findViewById(R.id.my_button).setOnClickListener(this);
    }

	@Override
	public void onClick(View arg0) {
		Button b = (Button)findViewById(R.id.my_button);
		b.setClickable(false);
		//ALERT MESSAGE
        Toast.makeText(getBaseContext(),"Sending Request, Please Wait...",Toast.LENGTH_LONG).show();
		new LongRunningGetIO().execute();
	}

	 
	
	
	
	public class LongRunningGetIO extends AsyncTask <Void, Void, String> {
		
  		

		private String android_id = Secure.getString(getBaseContext().getContentResolver(),
                Secure.ANDROID_ID);
		@Override
		protected String doInBackground(Void... params) {
			
		
			 HttpClient httpClient = new DefaultHttpClient();
			// HttpContext localContext = new BasicHttpContext();
             HttpPost httpPost = new HttpPost("http://192.168.1.100:8080/AndroidServisi/webresources/finki.contact");
             AutoCompleteTextView et = (AutoCompleteTextView)findViewById(R.id.latitudeText);
             AutoCompleteTextView email = (AutoCompleteTextView)findViewById(R.id.email);
             AutoCompleteTextView number = (AutoCompleteTextView)findViewById(R.id.number);
          
             String text = null;
                         try{
             JSONObject user = new JSONObject();
             try {

            	 user.put("name", et.getText().toString());
            	 user.put("email", email.getText().toString());
            	 user.put("phoneNumber", number.getText().toString());
                 user.put("reqID",android_id);
             } catch (Exception e) {
            	 return e.getLocalizedMessage();
             }
             // Create StringEntity
             StringEntity se = new StringEntity(user.toString());  
                 se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
                     httpPost.setEntity(se);     

             // Execute HTTP Post Request
             HttpResponse response = httpClient.execute(httpPost);

         } catch (ClientProtocolException cpe) {
             // TODO Auto-generated catch block
             cpe.printStackTrace();
         } catch (IOException ioe) {
             // TODO Auto-generated catch block
             ioe.printStackTrace();
         }
             return text;
		}	
		
		protected void onPostExecute(String results) {
			if (results!=null) {
				AutoCompleteTextView et = (AutoCompleteTextView)findViewById(R.id.latitudeText);
				et.setText(results);
				 Toast.makeText(getBaseContext(),"Server send error message",Toast.LENGTH_LONG).show();
			}else{
				 Toast.makeText(getBaseContext(),"Contact has been added",Toast.LENGTH_LONG).show();
			}
			Button b = (Button)findViewById(R.id.my_button);
			b.setClickable(true);
		}
    }
}